import vue from "@vitejs/plugin-vue";
import path from "path";
import { defineConfig } from "vite";
import postcssPxtoRem from 'postcss-pxtorem';
import { viteMockServe } from "vite-plugin-mock";
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  return {
    server: {
      host: "0.0.0.0",
      port: 4242,
      proxy: {
        "/api": {
          target: " http://1.95.13.110:8083",
          changeOrigin: true,
          bypass(req, res, options) {
            const proxyURL = options.target + options.rewrite(req.url);
            res.setHeader("x-req-proxyURL", proxyURL); // 将真实请求地址设置到响应头中
          },
          rewrite: (path) => path.replace(/^\/api/, "/api"),
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/style/variable.scss";',
        },
      },
      postcss: {
        plugins: [
          postcssPxtoRem({
            rootValue: 192, // 按照自己的设计稿修改 1440/10
            unitPrecision: 5, // 保留到5位小数
            propList: ['*'],
            selectorBlackList: ['.ant-',],
            replace: true,
            mediaQuery: false,
            minPixelValue: 1,
          }),
        ],
      },
    },

    plugins: [
      vue(),
      viteMockServe({
        mockPath: "mock",
        enable: command === "serve", //保证开发阶段可以使用mock接口
        logger: false,
      }),
    ],

    resolve: {
      alias: {
        "@": path.resolve("./src"),
      },
    },
  };
});
